package com.lvmama.rhino.web;


import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




import com.lvmama.rhino.entity.PushConditionVO;
import com.lvmama.rhino.pojo.PushData;
import com.lvmama.rhino.pojo.PushReturn;
import com.lvmama.rhino.service.PushService;
import com.lvmama.rhino.utils.FilenameUtils;


/**
 * 说明：
 * @author yecan
 * @date 2016年7月7日
 */

@Controller
@RequestMapping("push")
public class PushController {

	@Autowired
	private PushService pushService;
	
	@RequiresPermissions("sys:push:view")
	@RequestMapping("ajaxData")
	@ResponseBody
	public PushReturn ajaxData(PushConditionVO pushConditionVO){
		if(pushConditionVO.getPushObject().equals("all")){
			pushConditionVO.setPushObject(null);
		}
		if(pushConditionVO.getPushTerminal().equals("all")){
			pushConditionVO.setPushTerminal(null);
		}
		String[] split = pushConditionVO.getBtime().split("-");
		DateTimeFormatter formater = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
		Date startTime = DateTime.parse(split[0].trim()+ " 00:00:00", formater)
				.toDate();
		Date endTime = DateTime.parse(split[1].trim() + " 23:59:59", formater)
				.toDate();
		PushReturn pushReturn=pushService.queryPushData(pushConditionVO,startTime,endTime);
		return pushReturn;
	}
	
	@RequiresPermissions("sys:push:export")
	@RequestMapping("exportData")
	public void exportData(PushConditionVO pushConditionVO,HttpServletResponse response,HttpServletRequest request) throws IOException {
		if(pushConditionVO.getPushObject().equals("all")){
			pushConditionVO.setPushObject(null);
		}
		if(pushConditionVO.getPushTerminal().equals("all")){
			pushConditionVO.setPushTerminal(null);
		}
		String[] split = pushConditionVO.getBtime().split("-");
		DateTimeFormatter formater = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
		Date startTime = DateTime.parse(split[0].trim()+ " 00:00:00", formater)
				.toDate();
		Date endTime = DateTime.parse(split[1].trim() + " 23:59:59", formater)
				.toDate();
		List<PushData> list=pushService.exportPushData(pushConditionVO,startTime,endTime);
		
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		HSSFSheet sheet = hssfWorkbook.createSheet("推送数据");
		HSSFRow headerRow = sheet.createRow(0);
		headerRow.createCell(0).setCellValue("日期");
		headerRow.createCell(1).setCellValue("推送总量");
		headerRow.createCell(2).setCellValue("送达");
		headerRow.createCell(3).setCellValue("点击");
		headerRow.createCell(4).setCellValue("下单数");
		headerRow.createCell(5).setCellValue("订单金额");
		headerRow.createCell(6).setCellValue("支付单");
		headerRow.createCell(6).setCellValue("支付金额");
		
		for (PushData pushData : list) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			dataRow.createCell(0).setCellValue(pushData.getCreateTime().toLocaleString());
			dataRow.createCell(1).setCellValue(pushData.getPushTotalCount());
			dataRow.createCell(2).setCellValue(pushData.getPushDeliverCount());
			dataRow.createCell(3).setCellValue(pushData.getPushClickCount());
			dataRow.createCell(4).setCellValue(pushData.getOrderCount());
			dataRow.createCell(5).setCellValue(pushData.getOrderAmount());//char类型的数据问题
			dataRow.createCell(6).setCellValue(pushData.getPayCount());
			dataRow.createCell(6).setCellValue(pushData.getPayAmount());
		}
		
		String downloadFilename="推送数据.xls";
		String contentType=request.getSession().getServletContext().getMimeType(downloadFilename);
		response.setContentType(contentType);
		String agent=request.getHeader("user-agent");
		downloadFilename=FilenameUtils.encodeDownloadFilename(downloadFilename, agent);
		String contentDisposition="attachment;filename="+downloadFilename;
		response.setHeader("Content-Disposition", contentDisposition);
		hssfWorkbook.write(response.getOutputStream());
	}
	
}
